export function mockApi(data = {}, delay = 1000, shouldFail = false) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (shouldFail) {
        reject(new Error("API request failed!"));
      } else {
        resolve({ success: true, data });
      }
    }, delay);
  });
}

// 判断浏览器类型
export function checkBrowser() {
  const ua = navigator.userAgent.toLowerCase();
  if (ua.indexOf("micromessenger") !== -1) {
    return "WeChat Browser"; // 微信浏览器
  } else if (ua.indexOf("mobile") !== -1) {
    // return "WeChat Browser";
    return "Mobile Browser"; // 移动端本地浏览器
  } else {
    return "Desktop Browser"; // 桌面浏览器
  }
}

// 判断设备类型
export function detectDevice() {
  const userAgent = navigator.userAgent;
  let deviceType = "Unknown";
  if (/android/i.test(userAgent)) {
    deviceType = "Android";
  } else if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
    deviceType = "IOS";
  } else if (/Windows|Macintosh|Linux/.test(userAgent)) {
    deviceType = "Desktop";
  } else {
    deviceType = "Unknown";
  }
  return deviceType;
}
//   // 使用方式（try/catch 处理错误）
//   async function fetchData() {
//     try {
//       const response = await mockApi({ id: 1 }, 1000, false);
//       console.log(response);
//     } catch (error) {
//       console.error("Error:", error.message); // Error: API request failed!
//     }
//   }

//   fetchData();
